import { computed } from 'vue'
import { isArray } from '@/utils/is'
import { LoginStore } from '@/store/login'
import { intersection } from 'lodash'

const loginStore = LoginStore()

export const usePermission = () => {
  const permissions = computed(() => loginStore.permissionList || [])
  const hasPermission = (value?: string | string[]) => {
    if (!value) {
      return true
    }
    if (!isArray(value)) {
      return permissions.value.includes(value)
    }
    return intersection(value, permissions.value).length > 0
  }
  return { hasPermission }
}
